package com.zztl.infiniteBand.base

/**
 *
 */


import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import butterknife.ButterKnife
import butterknife.Unbinder
import com.trello.rxlifecycle.components.support.RxFragment
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.util.LogUtils
import com.zztl.infiniteBand.util.ToastUtils


/**
 *
 *
 */
abstract class BaseFragment : RxFragment() {


    private var mBind: Unbinder? = null

    protected abstract fun setLayoutId(): Int

    abstract fun initData()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initCreate()
    }

    open fun initCreate() {

    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(setLayoutId(), container, false)
        mBind = ButterKnife.bind(this, view)


        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        initData()
        super.onActivityCreated(savedInstanceState)
    }

    /**
     * show to @param(cls)ï¼Œbut can't finish activity
     */
    fun showActivity(cls: Class<*>) {
        val intent = Intent()
        intent.setClass(activity!!, cls)
        startActivity(intent)
        activity!!.overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right)
    }

    /**
     * show to @param(cls)ï¼Œbut can't finish activity
     */
    fun showActivity(i: Intent) {
        startActivity(i)
        activity!!.overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right)
    }

    fun showActivityForResult(intent: Intent, code: Int) {
        startActivityForResult(intent, code)
        activity!!.overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        LogUtils.i("MineFragment", "setUserVisibleHint---")
        if (isVisibleToUser && isVisible) {
            onVisibleToUser()
        }
        super.setUserVisibleHint(isVisibleToUser)
    }

    /**
     * 当前Fragment可见
     */
    open fun onVisibleToUser() {

    }

    /**
     *
     */
    fun showActivityForResult(cls: Class<*>, requestCode: Int) {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.setClass(activity!!, cls)
        this.startActivityForResult(intent, requestCode)
        activity!!.overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right)
    }


    /**
     * skip to @param(cls)ï¼Œand call @param(aty's) finish() method
     */
    fun skipActivity(cls: Class<*>) {
        showActivity(cls)
        activity!!.finish()
        activity!!.overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right)
    }

    override fun onDestroy() {
        super.onDestroy()
        mBind!!.unbind()
    }

    infix fun toast(msg:String){
        ToastUtils.show(activity!!,msg)
    }
}
